package ar.edu.sccs.model.loader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ar.edu.sccs.dao.DaoModule;
import ar.edu.sccs.dao.ProjectDTO;
import ar.edu.sccs.dao.RepositoryDTO;
import ar.edu.sccs.entities.Case;
import ar.edu.sccs.model.loader.dto.DataClazz;
import ar.edu.sccs.model.loader.dto.DataProject;
import ar.edu.sccs.model.loader.dto.DataSCM;
import ar.edu.sccs.monitoring.exception.SccsGenericException;
import ar.edu.sccs.monitoring.logging.SCCSLogger;
import ar.edu.sccs.util.Constants;
import ar.edu.sccs.util.FiltroDeArchivo;
import ar.edu.sccs.util.ObjectInStream;
import ar.edu.sccs.util.ObjectOutStream;
import ar.edu.sccs.util.SvnHelper;

public class CoreLoader {

	static Logger logger = SCCSLogger.getLogger(CoreLoader.class);

	private DaoModule daoModule = new DaoModule();

	public void loadProject() {
		ObjectInStream inputFile = new ObjectInStream();

		List<DataClazz> dataClazz = new ArrayList<DataClazz>();
		try {
			dataClazz = inputFile.readDataClazzFile(System.getProperty("jboss.server.home.dir") + Constants.Loader.FILE_NAME);
		} catch (SccsGenericException e) {
			logger.error("CoreLoader.loadProject ERROR", e);
		}

		List<Case> dataCases = ReflectionLoader.loadCaseInfo(dataClazz);
		ProjectDTO dataProject = ReflectionLoader.loadProjectInfo(dataClazz);
		RepositoryDTO dataReposity = ReflectionLoader.loadRepositoryInfo(dataClazz);
		try {
			Integer repositoryId = daoModule.findMaxRepositoryId();
			repositoryId = repositoryId == null ? 1 : repositoryId + 1;
			Integer projectId = daoModule.findMaxProjectId();
			projectId = projectId == null ? 1 : projectId + 1;
			dataReposity.setRepository_id(repositoryId);
			dataProject.setRepository_id(repositoryId);
			dataProject.setProject_id(projectId);
			daoModule.insertRepository(dataReposity);
			daoModule.insertProject(dataProject);
			daoModule.insertCases(dataCases, projectId);
		} catch (SccsGenericException e) {
			logger.error("CoreLoader.loadProject ERROR", e);
		}
	}

	public static void main(String[] args) {
		CoreLoader coreLoader = new CoreLoader();
		coreLoader.createMetaDataFile();
	}

	private void createMetaDataFile() {
		logger.debug("CoreLoader.createMetaDataFile - BEGIN");

		DataSCM clazzRepository = new DataSCM();
		DataProject clazzProject = new DataProject();

		ObjectInStream inputFile = new ObjectInStream();
		List<DataClazz> dataClazzList = new ArrayList<DataClazz>();
		try {
			dataClazzList = inputFile.readDataClazzFile(System.getProperty("jboss.server.home.dir") + Constants.Loader.FILE_NAME);
			clazzRepository = dataClazzList.get(0).getClazzRepository();
			clazzProject = dataClazzList.get(0).getClazzProject();
		} catch (SccsGenericException e) {
			logger.error("CoreLoader.createMetaDataFile ERROR", e);
		}

		List<String> clazzDirectory = loadProjectDirectory();
		List<String> clazzFileList = checkRepository(clazzRepository);

		DataClazz dataClazz;
		List<DataClazz> clazzData = new ArrayList<DataClazz>();
		for (String clazzFile : clazzFileList) {
			dataClazz = ReflectionLoader.getClassInfo(clazzProject, clazzRepository, clazzFile, clazzDirectory);
			if (dataClazz != null) clazzData.add(dataClazz);
		}

		ObjectOutStream outputFile = new ObjectOutStream();
		try {
			outputFile.writeDataClazzFile(System.getProperty("jboss.server.home.dir") + Constants.Loader.FILE_NAME, clazzData);
		} catch (Exception e) {
			logger.error("CoreLoader.createMetaDataFile ERROR", e);
		}

		logger.debug("CoreLoader.createMetaDataFile - END");
	}

	private List<String> checkRepository(DataSCM clazzRepository) {
		List<String> fileList = SvnHelper.recoverFilesProject(clazzRepository);
		logger.debug("CoreLoader.checkRepository - END");
		return fileList;
	}

	private List<String> loadProjectDirectory() {
		String projectDir = System.getProperty("user.dir");
		logger.debug("CoreLoader.loadProjectData from [" + projectDir + "]");

		List<String> sourceFolders = new ArrayList<String>();
		List<String> fileText = ObjectInStream.getContents(new File(projectDir + "/.classpath"));
		for (String string : fileText) {
			if (string.contains("classpathentry kind=\"src\""))
				sourceFolders.add(string.substring(string.lastIndexOf("=\"") + 2, string.lastIndexOf("\"")));
		}

		return sourceFolders;
	}

	// @Deprecated
	// private File loadProjectDirectory() {
	// File projectDir = new File(System.getProperty("user.dir"));
	// logger.debug("CoreLoader.loadProjectData from [" + projectDir + "]");
	//
	// return projectDir;
	// }

	@Deprecated
	private List<File> loadProjectFiles(File directory) {
		List<File> clazzFileList = new ArrayList<File>();

		for (File file : directory.listFiles(new FiltroDeArchivo("class", "COMMENT"))) {
			if (file.isFile())
				clazzFileList.add(file);
			else
				clazzFileList.addAll(this.loadProjectFiles(file));
		}

		return clazzFileList;
	}
}
